﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLHXNK.BusinessLogic;

namespace QLHXNK
{
    public partial class FrmNhapChiTietHang : Form
    {
        #region Properties
        int ImportNoteID = -1;
        int ImportNoteDetailID = -1;
        int ItemID = -1;

        /// <summary>
        /// 0:Thêm, 1: Xóa, 2: Sửa
        /// </summary>
        int action = -1;
        #endregion

        #region Form functions
        
        
        public FrmNhapChiTietHang()
        {
            InitializeComponent();
        }


       
        /// <summary>
        /// op = 0: thêm, 1: xóa, 2: sửa
        /// </summary>
        /// <param name="ind"></param>
        /// <param name="op"></param>
        public FrmNhapChiTietHang(Import_Note_Detail ind, int op, String soDonHang, String ngayDonHang, String tiGia)
        {
            InitializeComponent();

            lblNote.Visible = false;

            //
            ImportNoteDetailID = ind.ImportNoteDetailID;
            ImportNoteID = ind.ImportNoteID;
            action = op;
            
            //
            txtSoDonHang.Text = soDonHang;
            txtNgayDonHang.Text = ngayDonHang;
            txtTiGia.Text = tiGia;

            getDSHangHoa(getItemID(ImportNoteDetailID));

            if (action == 2)//Sửa
            {
                FillData(ImportNoteDetailID);
               
                btnLuu.Enabled = true;
                btnThem.Enabled = false;
                btnXoa.Enabled = false;
            }

            if (action == 0)//Thêm
            {
                ItemID = -1;
                cmbCIFCurrency.SelectedIndex = 0;
                cmbFOBCurrency.SelectedIndex = 0;
                btnXoa.Enabled = false;
                btnSua.Enabled = false;
            }

            //StateData:

           

        }
        private void cmbHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ItemID = int.Parse(cmbHangHoa.SelectedValue.ToString());
            }
            catch
            {
                ItemID = -1;
            }

            FillItemInfo(ItemID);
        }
        private void FrmNhapChiTietHang_Load(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void label3_Click_1(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void label4_Click_1(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_2(object sender, EventArgs e)
        {

        }

        private void label2_Click_2(object sender, EventArgs e)
        {

        }

        private void label3_Click_2(object sender, EventArgs e)
        {

        }

        private void label4_Click_2(object sender, EventArgs e)
        {

        }

        private void label5_Click_1(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged_2(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged_2(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click_3(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged_3(object sender, EventArgs e)
        {

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult resultDlg = MessageBox.Show("Bạn muốn xoá chi tiết đơn hàng này", "Xoá", MessageBoxButtons.YesNo);
            if (resultDlg == DialogResult.Yes)
            {
                if (ImportNoteDetailID != -1)
                {
                    if (Import_Note_Detail.Delete(ImportNoteDetailID) == 1)
                        ShowNote("Xóa thành công", Color.Blue);
                    else
                        ShowNote("Xóa không thành công", Color.Red);
                }
                else
                    ClearData();
                    
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ClearData();
            action = 0;
            ImportNoteDetailID = -1;
            ItemID = -1;

            btnXoa.Enabled = false;
            btnSua.Enabled = false;
        }
        #endregion

        #region Hàm hỗ trợ

        private void checkNET(ref TextBox giaNET, ref TextBox FobCif)
        {
            bool ret = true;
            Double dFobCif = 0;
            Double phiNuocBan = 0;
            Double phiNuocKhac = 0;
            Double vat = 0;
            Double ut = 0;
            Double tax = 0;
            Double khac = 0;

            //check txtFOB:
            if (FobCif.Text != "")
                if (ret = DoubleValidate(ref FobCif, 0, 0, false))
                    dFobCif = Double.Parse(FobCif.Text);


            //phi dich vu nuoc ngoai:
            if (txtPhiNuocBan.Text != "")
            {
                if (ret = DoubleValidate(ref txtPhiNuocBan, 0, 100, true))
                {
                    phiNuocBan = Double.Parse(txtPhiNuocBan.Text);
                }
            }

            if (txtPhiNuocKhac.Text != "")
            {
                if (ret = DoubleValidate(ref txtPhiNuocKhac, 0, 100, true))
                    phiNuocKhac = Double.Parse(txtPhiNuocKhac.Text);
            }

            //Phi o VN:
            if (txtVAT.Text != "")
                if (ret = DoubleValidate(ref txtVAT, 0, 100, true))
                    vat = Double.Parse(txtVAT.Text);
            if (txtUT.Text != "")
                if (ret = DoubleValidate(ref txtUT, 0, 100, true))
                    ut = Double.Parse(txtUT.Text);
            if (txtTAX.Text != "")
                if (ret = DoubleValidate(ref txtTAX, 0, 100, true))
                    tax = Double.Parse(txtTAX.Text);
            if (txtPhiKhac.Text != "")
                if (ret = DoubleValidate(ref txtPhiKhac, 0, 100, true))
                    khac = Double.Parse(txtPhiKhac.Text);


            if (ret)
            {
                Double net = dFobCif + (double)((phiNuocBan + phiNuocKhac + vat + ut + tax + khac) * dFobCif / 100 * 0.1);
                giaNET.Text = net.ToString();
            }
        }

        private void FillItemInfo(int itemID)
        {
            if (itemID != -1)
            {
                DataTable table = Item.getItemList(String.Format("SELECT * FROM Item WHERE ID = {0}", itemID));
                if (table != null && table.Rows.Count == 1)
                {
                    txtMaHang.Text = table.Rows[0]["item_code"].ToString();
                    txtTenHang.Text = table.Rows[0]["item_name"].ToString();
                    txtQuyCach.Text = table.Rows[0]["item_specs"].ToString();
                    txtDonViTinh.Text = table.Rows[0]["item_unit"].ToString();
                    txtGhiChu.Text = table.Rows[0]["remark"].ToString();
                    ItemID = (int)table.Rows[0]["ID"];
                }
            }
            else
            {
                txtMaHang.Text = "";
                txtTenHang.Text = "";
                txtQuyCach.Text = "";
                txtDonViTinh.Text = "";
                txtGhiChu.Text = "";
                ItemID = -1;
            }
        }

        private int getItemID(int importNoteDetailID)
        {
            DataTable table = MyDataTransfer.executeQueryCommand(String.Format("SELECT item_id FROM Import_Note_Detail WHERE ID = {0}", importNoteDetailID));
            if (table != null && table.Rows.Count == 1)
            {
                try
                {
                    return int.Parse(table.Rows[0]["item_id"].ToString());
                }
                catch
                {
                    return -1;
                }
            }

            return -1;

        }
        private void getDSHangHoa(int itemID)
        {
            //DS hang hoa:
           
            cmbHangHoa.DisplayMember = "info";
            cmbHangHoa.ValueMember = "ID";
            DataTable table = Item.getItemList("SELECT ID, item_code + ' - ' + item_name as info FROM Item");
            if (table == null)
            {
                table = new DataTable();
                table.Columns.Add("info");
                table.Columns.Add("ID");
            }

            DataRow row = table.NewRow();
            row["ID"] = -1;
            row["info"] = "Thêm hàng hóa mới";
            table.Rows.InsertAt(row, 0);

            cmbHangHoa.DataSource = table;


            cmbHangHoa.SelectedValue = itemID;
          
        }
        
        //private void getDSDonHang(int selectedImportNote)
        //{
        //    cmbDonHang.DataSource = Import_Note.getImport_NoteList(String.Format("SELECT ID, import_note_code FROM Import_Note"));
        //    cmbDonHang.DisplayMember = "import_note_code";
        //    cmbDonHang.ValueMember = "ID";

        //    if (selectedImportNote != -1)
        //    {
        //        cmbDonHang.SelectedValue = selectedImportNote;
        //    }
        //}

        //private void getDSChiTietDonHang(int importNoteID, int selectedImportNoteDetailID)
        //{
        //    MessageBox.Show("getDSChiTietDonHang " +selectedImportNoteDetailID.ToString());
        //    //DS chi tiet cua don hang
        //    cmbChiTietDonHang.DataSource = Import_Note_Detail.getImport_Note_DetailList(String.Format("SELECT ind.ID, i.item_code + ' - ' + i.item_name as info FROM Import_Note_Detail ind, Item i WHERE ind.item_id = i.ID AND ind.import_note_id = {0}", importNoteID));
        //    cmbChiTietDonHang.DisplayMember = "info";
        //    cmbChiTietDonHang.ValueMember = "ID";

        //    if (selectedImportNoteDetailID != -1)
        //        cmbChiTietDonHang.SelectedValue = selectedImportNoteDetailID;
        //}

        private void FillData(int importNoteDetailID)
        {

            if (importNoteDetailID != -1)
            {
                //item:
                FillItemInfo(getItemID(importNoteDetailID));
                //
                DataTable table = Import_Note_Detail.getImport_Note_DetailList(String.Format("SELECT * FROM Import_Note_Detail WHERE ID = {0}", importNoteDetailID));
                if (table != null && table.Rows.Count == 1)
                {
                    //fob:
                    if (table.Rows[0]["fob_eur"].ToString().Trim() != "")
                    {
                        txtFOB.Text = table.Rows[0]["fob_eur"].ToString();
                        cmbFOBCurrency.SelectedIndex = 1;
                    }
                    else
                    {
                        txtFOB.Text = table.Rows[0]["fob_usd"].ToString();
                        cmbFOBCurrency.SelectedIndex = 0;
                    }


                    //cif:
                    if (table.Rows[0]["cif_eur"].ToString().Trim() != "")
                    {
                        txtCIF.Text = table.Rows[0]["cif_eur"].ToString();
                        cmbCIFCurrency.SelectedIndex = 1;
                    }
                    else
                    {
                        txtCIF.Text = table.Rows[0]["cif_usd"].ToString();
                        cmbCIFCurrency.SelectedIndex = 0;
                    }

                    //Phi nuoc ngoai:
                    if (table.Rows[0]["kr_fee"].ToString() != "")
                    {
                        txtPhiNuocBan.Text = table.Rows[0]["kr_fee"].ToString();
                        cmbNuocBan.SelectedIndex = 0;
                    }
                    else
                        if (table.Rows[0]["hk_fee"].ToString() != "")
                        {
                            txtPhiNuocBan.Text = table.Rows[0]["hk_fee"].ToString();
                            cmbNuocBan.SelectedIndex = 1;
                        }
                        else
                            if (table.Rows[0]["ita_fee"].ToString() != "")
                            {
                                txtPhiNuocBan.Text = table.Rows[0]["ita_fee"].ToString();
                                cmbNuocBan.SelectedIndex = 2;
                            }
                            else
                            {
                                txtPhiNuocBan.Text = table.Rows[0]["tl_fee"].ToString();
                                cmbNuocBan.SelectedIndex = 3;
                            }

                    //nuoc khac:
                    txtPhiNuocKhac.Text = table.Rows[0]["other_country_fee"].ToString();

                    //phi trong nuoc
                    txtVAT.Text = table.Rows[0]["vat_fee"].ToString();
                    txtUT.Text = table.Rows[0]["ut_fee"].ToString();
                    txtTAX.Text = table.Rows[0]["tax_fee"].ToString();
                    txtPhiKhac.Text = table.Rows[0]["other_fee"].ToString();

                    //Gia, loi nhuan:
                    txtNET1.Text = table.Rows[0]["net1"].ToString();
                    txtNET2.Text = table.Rows[0]["net2"].ToString();
                    txtGiaBan.Text = table.Rows[0]["selling_price"].ToString();
                    txtProfit1.Text = table.Rows[0]["profit1"].ToString();
                    txtProfit2.Text = table.Rows[0]["profit2"].ToString();
                }
            }
            
        }
      
       
        private void ShowNote(string note, Color foreColor)
        {
            lblNote.Visible = true;
            lblNote.ForeColor = foreColor;
            lblNote.Text = note;
        }
        private void ClearData()
        {
            //Item info:
            FillItemInfo(-1);

            //FOB:
            txtFOB.Text = "";
            cmbFOBCurrency.SelectedIndex = 0;

           // CIF:
            txtCIF.Text = "";
            cmbCIFCurrency.SelectedIndex = 0;

            //Phi nuoc ban:
            txtPhiNuocBan.Text = "";
            cmbCIFCurrency.SelectedIndex = 0;

            //nuoc khac:
            txtPhiNuocKhac.Text = "";

            //phi trong nuoc
            txtVAT.Text = "";
            txtUT.Text = "";
            txtTAX.Text = "";
            txtPhiKhac.Text = "";

            //Gia, loi nhuan:
            txtNET1.Text = "";
            txtNET2.Text = "";
            txtGiaBan.Text = "";
            txtProfit1.Text = "";
            txtProfit2.Text = "";
        }


        private bool DoubleValidate(ref TextBox txt, Double min, Double max, bool minMaxCheck)
        {
            if (txt.Text != "")
            {
                try
                {
                    Double d = Double.Parse(txt.Text);
                    if (minMaxCheck == true)
                    {
                        if (d < min || d > max)
                        {
                            errorProvider1.SetError(txt, "Hãy nhập giá trị số trong khoảng " +min.ToString()+" đến "+max.ToString());
                            return false;
                        }
                    }

                    return true;
                }
                catch
                {
                    errorProvider1.SetError(txt, "Hãy nhập giá trị số");
                    return false;
                }
            }
            else
            {
                errorProvider1.SetError(txt, "");
                return true;
            }
        }

        private bool ValidateData()
        {
            bool isValidate = true;

            //Đơn hàng:
            //Số đơn hàng
            if (txtSoDonHang.Text == "")
            {
                errorProvider1.SetError(txtSoDonHang, "Bạn chưa nhập số đơn hàng");
                isValidate = false;
            }


            //Ngày đơn hàng
            if (txtNgayDonHang.Text == "")
            {
                errorProvider1.SetError(txtNgayDonHang, "Bạn chưa nhập ngày đơn hàng");
                isValidate = false;
            }
            else
            {
                try
                {
                    DateTime date = DateTime.Parse(txtNgayDonHang.Text);
                }
                catch
                {
                    errorProvider1.SetError(txtNgayDonHang, "Ngày đơn hàng không hợp lệ");
                    isValidate = false;
                }
            }

            //Tỉ giá:
            if (txtTiGia.Text != "")
            {
                try
                {
                    Double d = Double.Parse(txtTiGia.Text);
                }
                catch
                {
                    errorProvider1.SetError(txtTiGia, "Tỉ giá không hợp lệ");
                    isValidate = false;
                }
            }
           


            //Item info:
            //Mã hàng
            if (txtMaHang.Text == "")
            {
                errorProvider1.SetError(txtMaHang, "Bạn chưa nhập mã hàng");
                isValidate = false;
            }

            //Tên hàng
            if (txtTenHang.Text == "")
            {
                errorProvider1.SetError(txtTenHang, "Bạn chưa nhập tên hàng");
                isValidate = false;
            }

            //FOB:
            isValidate = DoubleValidate(ref txtFOB, 0, 0, false);
            

            // CIF:
            isValidate = DoubleValidate(ref txtCIF , 0, 0, false);
           
            //Phi nuoc ban:
            isValidate = DoubleValidate(ref txtPhiNuocBan , 0, 100, true);
           
            //nuoc khac:
            isValidate = DoubleValidate(ref txtPhiNuocKhac , 0, 100, true);

            //phi trong nuoc
            isValidate = DoubleValidate(ref txtVAT , 0, 100, true);
            isValidate = DoubleValidate(ref txtUT , 0, 100, true);
            isValidate = DoubleValidate(ref txtTAX , 0, 100, true);
            isValidate = DoubleValidate(ref txtPhiKhac , 0, 100, true);
            //Gia, loi nhuan:
            isValidate = DoubleValidate(ref txtNET1 , 0, 0, false);
            isValidate = DoubleValidate(ref txtNET2 , 0, 0, false);
            isValidate = DoubleValidate(ref txtGiaBan , 0, 0, false);
            isValidate = DoubleValidate(ref txtProfit1 , 0, 0, false);
            isValidate = DoubleValidate(ref txtProfit2 , 0, 0, false);

            return isValidate;
        }
        private void   ProcessData()
        {
            //Item:
            try
            {
                ItemID = int.Parse(cmbHangHoa.SelectedValue.ToString());
            }
            catch
            {
                ItemID = -1;
            }
            txtMaHang.Text = txtMaHang.Text.Trim();
            txtTenHang.Text = txtTenHang.Text.Trim();
            txtQuyCach.Text = txtQuyCach.Text.Trim();
            txtDonViTinh.Text =  txtDonViTinh.Text.Trim();
            txtGhiChu.Text = txtGhiChu.Text.Trim();

            //FOB:
            txtFOB.Text = txtFOB.Text.Trim();
           

            // CIF:
            txtCIF.Text = txtCIF.Text.Trim();
            
            //Phi nuoc ban:
            txtPhiNuocBan.Text = txtPhiNuocBan.Text.Trim();
           
            //nuoc khac:
            txtPhiNuocKhac.Text = txtPhiNuocKhac.Text.Trim();

            //phi trong nuoc
            txtVAT.Text =  txtVAT.Text .Trim();
            txtUT.Text = txtUT.Text.Trim();
            txtTAX.Text = txtTAX.Text.Trim();
            txtPhiKhac.Text =  txtPhiKhac.Text.Trim();
            //Gia, loi nhuan:
            txtNET1.Text = txtNET1.Text .Trim();
            txtNET2.Text = txtNET2.Text .Trim();
            txtGiaBan.Text = txtGiaBan.Text.Trim();
            txtProfit1.Text = txtProfit1.Text.Trim();
            txtProfit2.Text =  txtProfit2.Text.Trim();
        }

        #endregion

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Save();
        }

        #region Lưu
        private void Save()
        {
            lblNote.Visible = false;
            ProcessData();                  //Tiền xữ lý dữ liệu nhập(xử lý khoản trắng...)

            if (ValidateData() == false)
            {
                ShowNote("Dữ liệu không hợp lệ. Hãy kiểm tra lại!", Color.Red);
                return;                     //nếu dữ liệu nhập không hợp lệ thì thoát và thông báo
            }

            if (action == -1)
            {
                ShowNote("Bạn hãy chọn chức năng: Thêm mới, Xoá, Sửa", Color.Green);
                return;
            }

            //Thêm/Sửa:
            if (action == 0 || action == 2)
            {
                if (SaveDataOnForm() == 1)
                {
                    ShowNote("Lưu thành công", Color.Blue);
                    btnThem.Enabled = true;
                    btnXoa.Enabled = true;
                    btnSua.Enabled = true;
                }
                else
                    if (SaveDataOnForm() == 0)
                    {
                        ShowNote("Lưu không thành công. Xin thử lại!", Color.Red);
                    }
            }

            

        }

        private void checkPROFIT(ref TextBox NET, ref TextBox PROFIT)
        {
            bool ok = true;
            Double net = 0;
            Double giaBan = 0;

            if (txtGiaBan.Text.Trim() != "")
            {
                if (ok = DoubleValidate(ref txtGiaBan, 0, 0, false))
                {
                    giaBan = Double.Parse(txtGiaBan.Text.Trim());

                }

                if (NET.Text.Trim() != "")
                {
                    if (ok = DoubleValidate(ref NET, 0, 0, false))
                    {
                        net = Double.Parse(NET.Text.Trim());

                    }
                }
            }
            else
                ok = false;

            


            if (ok)
            {
                Double profit = giaBan - net;

                PROFIT.Text = profit.ToString();
            }
            else
            {
                PROFIT.Text = "";
            }

        }
        

        /// <summary>
        /// return -1: chua luu xong item, don hang, 0: luu chi tiet ko tcong, 1: thanh cong
        /// Nếu ID=-1: Thêm, ID>0: Sửa
        /// </summary>
        /// <returns></returns>
        private int SaveDataOnForm()
        {
            //Lưu đơn hàng mới neu có:
            if (ImportNoteID == -1)
            {
                int r = Import_Note.Insert(txtSoDonHang.Text, txtNgayDonHang.Text, txtTiGia.Text, txtGhiChu.Text);
                if (r == 1)
                {
                    ImportNoteID = MyDataTransfer.getLastID("Import_Note");
                }
            }
            else
            {
                int r = Import_Note.Update(ImportNoteID, txtSoDonHang.Text, txtNgayDonHang.Text, txtTiGia.Text, txtGhiChu.Text);
                
                if (r != 1)
                {
                    ShowNote("Chưa cập nhật được đơn hàng. Xin thử lại", Color.Red);
                    return -1;
                }
            }

            if (ImportNoteID == -1)
            {
                ShowNote("Chưa lưu được đơn hàng mới. Xin thử lại", Color.Red);
                return -1;
            }

            //Luu hang moi:
            if (ItemID == -1)
            {
                //Save item:
                int r = Item.Insert(txtMaHang.Text, txtTenHang.Text, txtQuyCach.Text, txtDonViTinh.Text, txtGhiChu.Text);
                if (r == 1)
                {
                    ItemID = MyDataTransfer.getLastID("Item");
                }

            }
            else
            {
                int r = Item.Update(ItemID, txtMaHang.Text, txtTenHang.Text, txtQuyCach.Text, txtDonViTinh.Text, txtGhiChu.Text);
                if (r != 1)
                {
                    ShowNote("Chưa cập nhật được hàng hóa mới. Xin thử lại", Color.Red);
                    return -1;
                }
            }

            if (ItemID == -1)//check if inserted
            {
                ShowNote("Chưa lưu được hàng hóa mới. Xin thử lại", Color.Red);
                return -1;
            }

            //Chuẩn bị dữ liệu:
            int import_note_id = ImportNoteID; int item_id = ItemID;
            String quantity = txtSoLuong.Text;
            String fob_usd = "";
            String fob_eur = "";
            if (txtFOB.Text != "")
            {
                if (cmbFOBCurrency.SelectedIndex == 0)
                    fob_usd = txtFOB.Text;
                else
                    fob_eur = txtFOB.Text;
            }

            String cif_usd = "";
            String cif_eur = "";
            if (txtCIF.Text != "")
            {
                if (cmbCIFCurrency.SelectedIndex == 0)
                {
                    cif_usd = txtCIF.Text;
                }
                else
                {
                    cif_eur = txtCIF.Text;
                }
            }

            
            String kr_fee = ""; String hk_fee = ""; String ita_fee = ""; String tl_fee = "";
            if (cmbNuocBan.SelectedIndex == 0 && txtPhiNuocBan.Text != "")
            {
                kr_fee = txtPhiNuocBan.Text;
            }

            if (cmbNuocBan.SelectedIndex == 1 && txtPhiNuocBan.Text != "")
            {
                hk_fee = txtPhiNuocBan.Text;
            }
            if (cmbNuocBan.SelectedIndex == 2 && txtPhiNuocBan.Text != "")
            {
                ita_fee = txtPhiNuocBan.Text;
            }
            if (cmbNuocBan.SelectedIndex == 3 && txtPhiNuocBan.Text != "")
            {
                tl_fee = txtPhiNuocBan.Text;
            }

            String other_country_fee = txtPhiNuocKhac.Text;

            String ut_fee = txtUT.Text;
            String vat_fee = txtVAT.Text;
            String tax_fee = txtTAX.Text;
            String other_fee = txtPhiKhac.Text;
            String net1 = txtNET1.Text;
            String net2 = txtNET2.Text;
            String selling_price = txtGiaBan.Text;
            String profit1 = txtProfit1.Text;
            String profit2 = txtProfit2.Text;


            //Lưu chi tiết:
            int result = 0;
            if (action == 0)
            {
                result = Import_Note_Detail.Insert(import_note_id, item_id, quantity, fob_usd, fob_eur, cif_usd, cif_eur, kr_fee, hk_fee, ita_fee, tl_fee, other_country_fee, ut_fee, vat_fee, tax_fee, other_fee, net1, net2, selling_price, profit1, profit2);
            }
            else
                if (action == 2 && ImportNoteDetailID != -1)
                    result = Import_Note_Detail.Update(ImportNoteDetailID, import_note_id, item_id, quantity, fob_usd, fob_eur, cif_usd, cif_eur, kr_fee, hk_fee, ita_fee, tl_fee, other_country_fee, ut_fee, vat_fee, tax_fee, other_fee, net1, net2, selling_price, profit1, profit2);
            // int result = Import_Note_Detail.Update(ImportNoteDetailID, import_note_id, item_id, txtSoLuong.Text, fob_usd, fob_eur, cif_usd, cif_eur, kr_fee, hk_fee, ita_fee, tl_fee, txtPhiNuocKhac.Text, txtUT.Text, txtVAT.Text, txtTAX.Text, txtPhiKhac.Text, txtNET1.Text, txtNET2.Text, txtGiaBan.Text, txtProfit1.Text, txtProfit2.Text);
            if (result == 1)
                return 1;
            else
                return 0;

        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtFOB_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1,ref txtFOB);
        }
        
        private void txtCIF_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtPhiNuocBan_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1, ref txtFOB);
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtPhiNuocKhac_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1, ref txtFOB);
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtUT_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1, ref txtFOB);
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtVAT_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1, ref txtFOB);
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtTAX_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1, ref txtFOB);
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtPhiKhac_TextChanged(object sender, EventArgs e)
        {
            checkNET(ref txtNET1, ref txtFOB);
            checkNET(ref txtNET2, ref txtCIF);
        }

        private void txtGiaBan_TextChanged(object sender, EventArgs e)
        {
            checkPROFIT(ref txtNET1, ref  txtProfit1);
            checkPROFIT(ref txtNET2, ref  txtProfit2);
        }

        private void txtNET1_TextChanged(object sender, EventArgs e)
        {
            checkPROFIT(ref txtNET1, ref  txtProfit1);
        }

        private void txtNET2_TextChanged(object sender, EventArgs e)
        {
            checkPROFIT(ref txtNET2, ref  txtProfit2);
        }

        private void cmbCIFCurrency_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //private void cmbDonHang_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    MessageBox.Show("cmbDonHang_SelectedIndexChanged");
        //    int importNoteID = -1;
        //    try
        //    {
        //        importNoteID = int.Parse(cmbDonHang.SelectedValue.ToString());
        //    }
        //    catch
        //    {
        //        importNoteID = -1;
        //    }

        //    getDSChiTietDonHang(importNoteID, ImportNoteDetailID);
        //}

        //private void cmbChiTietDonHang_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ImportNoteDetailID =(int) cmbChiTietDonHang.SelectedValue;

        //    ItemID = getItemID(ImportNoteDetailID);

        //    cmbHangHoa.SelectedValue = ItemID;

        //    FillData(ImportNoteDetailID);
        //}
    }
}